const express = require("express");

const userRouter = require("../route/user.router")
const authRouter = require("../route/auth.router")
const testRouter = require("../route/test")
const momentRouter = require("../route/moment.router")
// const { userMiddleWare } = require("../middleware/user.middleware")
const {userError} = require("./error-handle")
const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// app.use((err, req, res, next) => {
//   console.log(err.message, "ooo")
//   res.json(err.message)
// })
// 解决跨域
app.all('*', function(req, res, next) {
  // console.log(req.method);
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Headers', 'Content-type');
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS,PATCH");
  res.header('Access-Control-Max-Age',1728000);//预请求缓存20天
  next();  
});
app.use("/register", userRouter)
app.use("/login", authRouter)
app.use("/", testRouter)
app.use("/", momentRouter)







// 直接写在这里面的
// app.use((err, req, res, next) => {
//   console.log(err.message, "ooo")
//   res.json({
//     errmsg: err.message
//   })
// })
// 错误中间件 抽离出去了
app.use(userError)



module.exports = app;